﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using Vit.Web.Extensions;

namespace Vit.Mvc
{
    public class LocalizedModelValidatorProvider : DataAnnotationsModelValidatorProvider
    {
        protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
        {
            foreach (ValidationAttribute attribute in attributes.OfType<ValidationAttribute>())
            {
                if (!string.IsNullOrWhiteSpace(attribute.ErrorMessage))
                {
                    attribute.ErrorMessage = attribute.ErrorMessage.Locale();
                }
            }
            var validators = base.GetValidators(metadata, context, attributes);
            return validators;
        }
    }
}
